package test_package;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;

import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 22.11.13
 * Time: 13:23
 * To change this template use File | Settings | File Templates.
 */
public class buxfer_com_PO_AccountPage {
    private WebDriver driver;

    public buxfer_com_PO_AccountPage(WebDriver driver) {
        this.driver = driver;

    }

    public buxfer_com_PO_AccountPage closePopUpWindow() {
        driver.findElement(By.xpath("//*[@id=\"overlay\"]/table/tbody/tr[3]/td/div[1]/div/img")).click();
        return new buxfer_com_PO_AccountPage(driver);
    }

    public buxfer_com_PO_AccountPage clickOnMenu() {
        driver.findElement(By.xpath("//div[contains(text(),'Account')]")).click();
        return new buxfer_com_PO_AccountPage(driver);

    }

    public buxfer_com_PO_AccountPage clickOnMenuItem(String s) {
        driver.findElement(By.className(String.format("//div[contains(text(),%s)]", s))).click();
        return new buxfer_com_PO_AccountPage(driver);
    }

    public boolean findText(String sl, String s) {
        return driver.findElement(By.xpath(sl)).getText().contains(s);
    }

    public void takeScreenshot(String s) throws IOException {
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File(String.format("c:\\%s%d.png", s, System.currentTimeMillis())));

    }

    public buxfer_com_PO_AccountPage delete_account(String login, String password) {
        driver.get("https://www.buxfer.com");
        driver.findElement(By.id("login_username")).sendKeys(login);
        driver.findElement(By.id("login_password")).sendKeys(password);
        driver.findElement(By.xpath("//a[contains(text(),'Login')]")).click();
        driver.get("https://www.buxfer.com/settings?type=6");
        WebElement input = driver.findElement(By.xpath("//a[contains(text(),'Yes, delete my account')]"));
        new Actions(driver).moveToElement(input).click().perform();
        driver.findElement(By.name("password")).sendKeys(password);
        driver.findElement(By.name("reason")).sendKeys("In test purpose");
        driver.findElement(By.xpath("//a[contains(text(),'Delete my account')]")).click();
        return new buxfer_com_PO_AccountPage(driver);
    }
}
